package cn.jzk.mySpringSecurity_noSeparation.SecurityHandler;

import cn.jzk.mySpringSecurity_noSeparation.Common.Enums.AppHttpCodeEnum;
import cn.jzk.mySpringSecurity_noSeparation.Common.Result.ResponseResult;
import cn.jzk.mySpringSecurity_noSeparation.Common.Utils.MyJsonUtils;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.http.MediaType;
import org.springframework.security.core.Authentication;
import org.springframework.security.web.authentication.logout.LogoutSuccessHandler;

import java.io.IOException;
import java.io.PrintWriter;

/**
 * 登录用户登录成功后【注销】响应处理器
 *  @author jiangzk
 */
public class MyLogoutSuccessHandler implements LogoutSuccessHandler {

    @Override
    public void onLogoutSuccess(HttpServletRequest request, HttpServletResponse response,
                                Authentication authentication) throws IOException{

        // 设置响应的内容类型和字符集
        response.setContentType(MediaType.APPLICATION_JSON_VALUE);
        response.setCharacterEncoding("UTF-8");

        // 返回自定义的 JSON 响应信息
        try (PrintWriter writer = response.getWriter()) {
            writer.print(MyJsonUtils.stringify(
                    ResponseResult.okResult(AppHttpCodeEnum.SUCCESS.getCode(), "退出登录成功")));
        }
    }
}
